package cn.donzell.app.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class NameGenerator {
	private static List<String> firstNames;// 名
	private static List<String> lastNames;// 姓

	static {
		firstNames = readFile("firstname.txt");
		lastNames = readFile("lastname.txt");
	}

	/**
	 * 生成姓名
	 * 
	 * @return
	 */
	public static String generate() {
		Random random = new Random();
		int index = random.nextInt(lastNames.size() - 1);
		String name = lastNames.get(index);
		// nextBoolean() 方法用于从该随机数生成器的序列得到下一个伪均匀分布的布尔值。
		// 根据随机布尔值，确定名字是一个字还是两个字
		if (random.nextBoolean()) {
			name += getName() + getName();
		} else {
			name += getName();
		}
		return name;
	}

	public static void main(String[] args) {
		System.out.println(generate());
	}

	/**
	 * 随机获取一个汉字来组成名字
	 * 
	 * @return
	 */
	public static String getName() {
		Random random = new Random();
		int index = random.nextInt(firstNames.size() - 1);
		return firstNames.get(index);
	}

	private static List<String> readFile(String fileName) {
		List<String> results = new ArrayList<String>();
		List<String> datas = ExcelUtils.readData(fileName);
		for (String line : datas) {
			String[] arr = line.split("");
			for (int i = 0; i < arr.length; i++) {
				results.add(arr[i]);
			}
		}
		return results;
	}

}
